<template>
  <div>
    <el-card>
      <div class="search_box">
        <el-row :gutter="20">
          <el-form size="mini" :model="searchForm" label-width="80px">
            <el-col :span="5">
              <el-form-item label="用户名">
                <el-input
                  clearable
                  @clear="handleSearch"
                  @keyup.enter.native="handleSearch"
                  v-model="searchForm.username"
                  placeholder="请输入用户名"
                  auto-complete="false"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="ip地址">
                <el-input
                  @clear="handleSearch"
                  @keyup.enter.native="handleSearch"
                  v-model="searchForm.ip"
                  placeholder="请输入ip地址"
                  auto-complete="false"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="登入地址">
                <el-input
                  @clear="handleSearch"
                  @keyup.enter.native="handleSearch"
                  v-model="searchForm.location"
                  placeholder="请输入登入地址"
                  auto-complete="false"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="handleSearch"
                >查询</el-button
              >
              <el-button icon="el-icon-delete" @click="handleReset"
                >重置</el-button
              >
              <el-button
                icon="el-icon-delete"
                :disabled="delDisabled"
                @click="handleDels"
                v-permission="'log:batchDelete'"
                >删除</el-button
              >
            </el-col>
          </el-form>
        </el-row>
      </div>
      <div class="table_box">
        <el-table
          size="small"
          :data="LogsList"
          border
          stripe
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column
            width="80"
            show-overflow-tooltip
            align="center"
            prop="operation"
            label="操作"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="method"
            label="方法"
            show-overflow-tooltip
            width="200"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="params"
            label="参数"
            width="200"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            prop="time"
            label="耗时"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="location"
            label="操作地点"
            width="170"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            prop="ip"
            label="IP地址"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="username"
            label="操作人"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="createTime"
            label="时间"
            width="170"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column align="center" prop="" label="操作">
            <template scope="scope">
              <el-button
                type="text"
                @click="del(scope.row.id)"
                icon="el-icon-delete"
                v-permission="'log:delete'"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pageTools
        :total="total"
        :paginationPage="searchForm.pageNum"
        :paginationPagesize="searchForm.pageSize"
        @pageChange="pageChange"
        @pageSizeChange="pageSizeChange"
      ></pageTools>
    </el-card>
  </div>
</template>

<script>
import { getlogsListApi, dellogApi, delsLogsApi } from '../../api/logs'
export default {
  data () {
    return {
      searchForm: {
        pageNum: 1,
        pageSize: 5,
        location: '',
        ip: '',
        username: ''
      },
      LogsList: [],
      total: 0,
      checkedList: []
    }
  },
  methods: {
    handleDels () {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.onDeletes()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async onDeletes () {
      const res = await delsLogsApi(this.checkedList)
      if (!res.success) return this.$notify.error(res.data.errorMsg)
      this.getLogsList()
      this.$notify.success('删除成功')
    },
    // 获取全选的val
    handleSelectionChange (val) {
      this.checkedList = val.map((item) => item.id)
    },
    handleReset () {
      this.searchForm = {
        pageNum: 1,
        pageSize: 5,
        location: '',
        ip: '',
        username: ''
      }
    },
    handleSearch () {
      this.searchForm.pageNum = 1
      this.getLogsList()
    },
    async getLogsList () {
      const res = await getlogsListApi(this.searchForm)
      if (!res.success) return this.$notify.error(res.data.errorMsg)
      this.LogsList = res.data.rows
      this.total = res.data.total
    },
    del (id) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.onDelete(id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async onDelete (id) {
      const res = await dellogApi(id)
      if (!res.success) return this.$notify.error(res.data.errorMsg)
      this.getLogsList()
      this.$notify.success('删除成功')
    },
    pageChange (val) {
      this.searchForm.pageNum = val
      this.getLogsList()
    },
    pageSizeChange (val) {
      this.searchForm.pageSize = val
      this.getLogsList()
    }
  },
  created () {
    this.getLogsList()
  },
  computed: {
    delDisabled () {
      if (this.checkedList.length === 0) {
        return true
      } else {
        return false
      }
    }
  }
}
</script>

<style>
</style>
